package com.itcoon.transform4j.demo.web.controller;

import com.itcoon.common.web.core.response.ApiResponse;
import com.itcoon.transform.starter.Transformer;
import com.itcoon.transform4j.demo.domain.Student;
import com.itcoon.transform4j.demo.domain.Teacher;
import com.itcoon.transform4j.demo.web.vo.TeacherVCO;
import com.itcoon.transform4j.demo.web.vo.TeacherVIO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "10_inject_提高篇", description = "inject 测试")
@RestController
@RequestMapping("/api")
public class ComplexInjectController {

    @ApiOperation("教师列表——批量inject, 目标接口定义")
    @GetMapping("teachers1")
    public ApiResponse<List<TeacherVIO>> listTeachers(){
        Teacher teacher1 = new Teacher();
        teacher1.setId(1L);
        teacher1.setName("安东老师");

        Student student1 = new Student();
        student1.setName("黄丽莉");
        student1.setSid(1L);
        teacher1.setStudent(student1);

        Teacher teacher2 = new Teacher();
        teacher2.setId(2L);
        teacher2.setName("闵老师");

        Student student2 = new Student();
        student2.setName("刘发发");
        student2.setSid(2L);
        teacher2.setStudent(student2);

        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);

        List<TeacherVIO> result = Transformer
                .toList(TeacherVIO.class)
                .apply(teachers)
                .setAt("name", name->"变异 " + name)
                .setAt("student.name",name->"小"+name)
                .done();
        return ApiResponse.createSuccess(result);
    }

    @ApiOperation("教师列表——批量inject, 目标类定义")
    @GetMapping("teachers2")
    public ApiResponse<List<TeacherVCO>> listTeachers2(){
        Teacher teacher1 = new Teacher();
        teacher1.setId(1L);
        teacher1.setName("安东老师");

        Student student1 = new Student();
        student1.setName("黄丽莉");
        student1.setSid(1L);
        teacher1.setStudent(student1);

        Teacher teacher2 = new Teacher();
        teacher2.setId(2L);
        teacher2.setName("闵老师");

        Student student2 = new Student();
        student2.setName("刘发发");
        student2.setSid(2L);
        teacher2.setStudent(student2);

        List<Teacher> teachers = new ArrayList<>();
        teachers.add(teacher1);
        teachers.add(teacher2);

        List<TeacherVCO> result = Transformer
                .toList(TeacherVCO.class)
                .apply(teachers)
                .setAt("name", name->"变异 " + name)
                .setAt("student.name",name->"小"+name)
                .done();
        return ApiResponse.createSuccess(result);
    }


}
